// 宿主小程序->插件的功能封装

const config = require('./config')
const Event = require('./event')
const constDefine = require('./constDefine')

module.exports = { 
    hostConfig: config,
    wxNavigateToMiniProgram: wx.navigateToMiniProgram,
    wxNavigateTo: wx.navigateTo,
    wxNavigateBack: wx.navigateBack,
    wxRedirectTo: wx.redirectTo,
    wxReLaunch: wx.reLaunch,
    wxSwitchTab: wx.switchTab,
    wxNavigateBack: wx.navigateBack,
    wxPageScrollTo: wx.pageScrollTo,
    wxSetNavigationBarColor: wx.setNavigationBarColor,
    wxSetNavigationBarTitle: wx.setNavigationBarTitle
}

// 借助于宿主小程序共享存储信息
const __PLUGIN_AUTH_KEY__ = `__${config.TAG}_${config.SHOP_ID}_auth_key__`
const __PLUGIN_SID_KEY__ = `__${config.TAG}_${config.SHOP_ID}_sid_key__`
const __PLUGIN_SHARE_USER_ID_KEY__ = `__${config.TAG}_${config.SHOP_ID}_share_userId_key__`
Object.assign(module.exports, {
    saveStorageForPluginAuth: function(res) {
        try { 
            wx.setStorageSync(__PLUGIN_AUTH_KEY__, res) 
        } catch(e){
            console.error("PLUGIN ERROR! save auth storage failed:", e)
        }
    },
    getStorageForPluginAuth: function() {
        return uni.getStorageSync(__PLUGIN_AUTH_KEY__)
    },
    clearStorageForPluginAuth: function() {
        wx.removeStorage({ key: __PLUGIN_AUTH_KEY__ })
        wx.removeStorage({ key: __PLUGIN_SID_KEY__ })
        wx.removeStorage({ key: __PLUGIN_SHARE_USER_ID_KEY__ })
    },
    saveStorageForPluginSid: function(res) {
        try { 
            wx.setStorageSync(__PLUGIN_SID_KEY__, res) 
        } catch(e){
            console.error("PLUGIN ERROR! save sid storage failed:", e)
        }
    },
    getStorageForPluginSid: function() {
        return wx.getStorageSync(__PLUGIN_SID_KEY__)
    },
    saveStorageForPluginShareUserId: function(res) {
        try { 
            wx.setStorageSync(__PLUGIN_SHARE_USER_ID_KEY__, res) 
        } catch(e){
            console.error("PLUGIN ERROR! save shareUserId storage failed:", e)
        }
    },
    getStorageForPluginShareUserId: function() {
        return wx.getStorageSync(__PLUGIN_SHARE_USER_ID_KEY__)
    },

})

// 不需要修改，借助于宿主小程序分发事件通知
module.exports.getEventBus = function() {
    const app = getApp()
    if (!app) {
        return null
    }

    const varName = constDefine.SHOPPLUGIN_VARNAME_APP_EVENTBUS_NAME
    if (!app[varName]) {
        app[varName] = new Event()
    }

    return app[varName]
}

module.exports.getComponentsShareData = function() {
    const app = getApp()
    if (!app) {
        return null
    }

    const varName = constDefine.SHOPPLUGIN_VARNAME_APP_SHARE_DATA_NAME

    return app[varName]
}

module.exports.setComponentsShareData = function(obj) {
    const app = getApp()
    if (!app) {
        return null
    }

    const varName = constDefine.SHOPPLUGIN_VARNAME_APP_SHARE_DATA_NAME

    app[varName] = obj
}

// 不可修改，宿主小程序提供的中转页文件夹路径
module.exports.hostFoldPath = '/static/shopPlugin/shopcore'
module.exports.hostFoldPath2 = '/static/shopPlugin/shoptab'

// TODO：需要修改为宿主小程序登陆页面路径
module.exports.loginPagePath = '/subPackages/login/wxAuth' 

// TODO：需要修改为宿主小程序tab首页页面路径
module.exports.homePath = '/pages/index/index'

// TODO：需要修改为宿主小程序tab个人中心页面路径
module.exports.userCenterPath = '/pages/user/index'

// TODO：需要修改为宿主小程序客服url地址
module.exports.contactUrl = 'https://work.weixin.qq.com/kfid/kfccd415c6aed8d47cd'

// TODO：商品详情是否分享二维码，需要对接生成小程序二维码接口
module.exports.itemCanShareQRCode = false
module.exports.topToolbarShare = true